/*
 * Copyright 2008. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the Apache License, Version 2.0. You
 * can find a copy of the license at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */

package com.sinai.mshab.extensions.security;

import java.util.Date;

import com.google.gwt.user.client.Cookies;

/**
 * This class stores the cookies with the user's sessionID that was given by the
 * server.
 * 
 * @author serge
 * 
 */
public class Security {
	
	private static boolean loggedIn = false;

	/**
	 * Creates a cookie remembering the user's sessionID.
	 * 
	 * @param sessionID
	 *            the session ID given by the server.
	 * @param duration
	 *            the duration in hours
	 */
	public static void rememberLogin(String sessionID, int duration) {
		final int expire_duration = 1000 * 60 * 60;
		Date expires = new Date(System.currentTimeMillis() + expire_duration);
		Cookies.setCookie("sid", sessionID, expires, null, "/", false);
	}

	/**
	 * Returns the sessionID associated with the user, or null if the cookie
	 * expired or does not exist.
	 * 
	 * @return the sessionID associated with the user, or null
	 */
	public static String getSessionID() {
		return Cookies.getCookie("sid");
	}
	
	/**
	 * Removes any cookies associated with this page.
	 */
	public static void logOff() {
		setLoggedIn(false);
		Cookies.removeCookie("sid");
	}
	
	/**
	 * @return Returns if true if the user is logged in.
	 */
	public static boolean isLoggedIn() {
		return loggedIn;
	}
	
	/**
	 * @param log the new value of logged in.
	 */
	public static void setLoggedIn(boolean log) {
		loggedIn = log;
	}
}
